package com.erhya.export.poi.handler;

/**
 * @Author: lilin
 * @Date: 2022/4/16
 */
public class HandlerChain {

    /** 头部节点 */
    private ExHandler head;

    /** 下一节点指向 */
    private ExHandler tail;

    public void addHandler(ExHandler handler){
        handler.setSuccessor(null);

        // 设置首个节点
        if (head == null){
            head = handler;
            tail = handler;
            return;
        }

        // 设置上一节点为当前节点
        tail.setSuccessor(handler);
        tail = handler;
    }

    public void handler(){
        System.out.println("拦截器链开始...");

        if (head != null){
            head.handle();
        }

        System.out.println("拦截器链结束...");
    }


    public static void main(String[] args) {
        HandlerChain chain = new HandlerChain();
        chain.addHandler(new HandlerB());
        chain.addHandler(new HandlerA());
        chain.addHandler(new HandlerC());
        chain.handler();
    }

}
